package vns.server;

import javax.jdo.PersistenceManager;

import vns.client.service.RuleActionService;
import vns.server.domain.RuleAction;
import vns.shared.RuleActionDTO;
import com.google.appengine.api.datastore.Key;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

@SuppressWarnings("serial")
public class RuleActionServiceImpl extends RemoteServiceServlet implements RuleActionService{

	@Override
	public RuleActionDTO Create(RuleActionDTO ruleActionDTO) {
		PersistenceManager pm=PMF.get().getPersistenceManager();
		RuleAction ruleAction=null;
		try{
			ruleAction=new RuleAction(ruleActionDTO);
			pm.makePersistent(ruleAction);
		}finally{
			pm.close();
		}
		return ruleAction.toDTO();
	}

	@Override
	public RuleActionDTO Load(Key id) {
		PersistenceManager pm=PMF.get().getPersistenceManager();
		RuleActionDTO ruleActionDTO=null;
		try{
			RuleAction ruleAction = pm.getObjectById(RuleAction.class,id);	
			ruleActionDTO=ruleAction.toDTO();
		}finally{
			pm.close();
		}
		return ruleActionDTO;
	}

	@Override
	public RuleActionDTO Update(RuleActionDTO ruleActionDTO) {
		PersistenceManager pm=PMF.get().getPersistenceManager();
		RuleAction ruleAction=null;
		try{
			ruleAction=pm.getObjectById(RuleAction.class,ruleActionDTO.getId());
			ruleAction.updateFromDTO(ruleActionDTO);
		}finally{
			pm.close();
		}
		
		return ruleAction.toDTO();
	}

	@Override
	public boolean Remove(Key id) {
		PersistenceManager pm=PMF.get().getPersistenceManager();
		try{
			RuleAction ruleAction= pm.getObjectById(RuleAction.class,id);
			if(ruleAction!=null){
				pm.deletePersistent(pm.getObjectById(RuleAction.class,id));
			}
		}finally{
			pm.close();
		}
		return true;
	}

}
